package com.hi.hiparty.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hi.hiparty.R;
import com.hi.hiparty.ui.session.UserSession;
import com.hi.hiparty.ui.session.UserSessionManager;
import com.hi.hiparty.util.ApiHelper;
import com.hi.hiparty.util.HttpUtil;
import com.hi.hiparty.util.StringUtils;

public class LoginActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

	}

	public void onLoginButtonSubmit(View view) {
		EditText loginnameTextInput = (EditText) findViewById(R.id.edit_text_loginname);
		EditText passwordTextInput = (EditText) findViewById(R.id.edit_text_loginpwd);

		String name = loginnameTextInput.getText().toString();
		String pwd = passwordTextInput.getText().toString();

		if (StringUtils.isBlank(name)) {
			showErrorMessageToast("登陆名不能为空");
			return;
		}

		if (!name.matches("^[a-zA-Z0-9_]*")) {
			showErrorMessageToast("登陆名只能包含字母和数字");
			return;
		}

		if (pwd.length() < 6) {
			showErrorMessageToast("密码长度都是超过6位数的");
			return;
		}

		LoginAsyncTask task = new LoginAsyncTask();
		task.execute(name, pwd);
	}

	public class LoginAsyncTask extends AsyncTask<String, Void, Boolean> {

		private ProgressDialog dialog;

		private String errorMessage;

		private String userid;

		protected void onPreExecute() {
			dialog = ProgressDialog.show(LoginActivity.this, "", "正在发送登陆信息...",
					true);
		}

		protected void onPostExecute(final Boolean success) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (success) {
				// 进入登陆后主页
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("userid", userid);
				intent.putExtras(bundle);
				startActivity(intent);
			} else {
				Toast.makeText(LoginActivity.this, errorMessage,
						Toast.LENGTH_LONG).show();
			}
		}

		protected Boolean doInBackground(final String... args) {
			if (null == args || args.length < 2) {
				return false;
			}
			String loginname = args[0];
			String password = args[1];
			String url = ApiHelper.API_LOGIN + loginname + "/" + password;
			String jsonString = HttpUtil.GET(url);
			if (StringUtils.isBlank(jsonString)) {
				errorMessage = "您的网络信号不太好";
				return false;
			}
			try {
				JSONObject json = new JSONObject(jsonString);
				if (json.getInt("success") == 1) {
					userid = json.getString("userid");
					UserSession session = UserSessionManager.createNewSession();
					session.setUserid(userid);
					UserSessionManager.saveUserSession(LoginActivity.this);
					return true;
				} else {
					errorMessage = json.getString("error");
					return false;
				}
			} catch (JSONException e) {
				errorMessage = "服务请求格式异常，请稍后再试";
				return false;
			}
		}
	}

	private void showErrorMessageToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
}
